// ==UserScript==
// @name          Blog Picasa Images
// @namespace     http://wtwf.com/
// @description	  Make a new window with picasa images in it
// @include       http://picasaweb.google.com/*

// Copyright 2007 wtwf.com, All Rights Reserved.
// Created by: Alex K (wtwf.com)

var RCS_REVISION = "$Revision: 1.6 $";

// ==/UserScript==

(function() {
  function getUploadButton() {
    var els = document.getElementsByTagName('div');
    if (els) {
      for (var i = 0; i < els.length; i++) {
        if (els[i].className == 'buttonlabel' &&
            els[i].firstChild.data == 'Upload') {
          return els[i];
        }
      }
    }
  }

  function addButton() {
    var upload_button = getUploadButton();
    if (upload_button) {
      var par = upload_button;
      while (par != null && par.nodeName != 'TD') {
        par = par.parentNode;
      }
      var td = document.createElement('td');

      var lab = document.createTextNode(" Blog");
      var but = document.createElement("div");

      but.addEventListener('click', function(event) {
          CreateBlogPost();
        }, true);

      but.appendChild(lab);
      td.appendChild(but);
      par.parentNode.insertBefore(td, par.nextSibling);
    }

  }

  function GetPicasaImage(src) {
    // http://lh3.ggpht.com/_PI1DFKRrwUU/St5ODfXDxxI/AAAAAAAAQ5c/ugYG30oqzzA/s288/IMG_9988.JPG
    var parts = src.split('/');
    if (parts.length == 9) {
      if (parts[7].substring(0, 1) == 's' &&
          Number(parts[7].substring(1)) > 0) {
        parts[7] = 's288';
        return parts.join('/');
      }
    }
    return null;
  }

  function CreateBlogPost() {
    var els = document.getElementsByTagName('img');
    if (els) {
      var nw = window.open();
      var nd = nw.document;
      var txt = '';
      for (var i = 0; i < els.length; i++) {
        var src = GetPicasaImage(els[i].src);
        if (src) {
          nd.write('\n<div style="clear: both; margin-bottom: 10px;"><img style="margin-bottom: 10px; margin-right: 10px; float: left;" alt="" src="' + src + '" />\n\n</div>');
        }
      }
      nd.write('\n<div style="clear: both; margin-bottom: 10px;">More pictures in the <a href="' + document.location + '">Gallery<\a></div>\n');
      nd.close();
    }
  }


  setTimeout(addButton, 1000);


})();
